package org.video.mine.fragment.home;

import androidx.core.view.ViewCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.flexbox.FlexboxLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import org.video.mine.HomeDocModel;
import org.video.mine.R;
import org.video.mine.Urls;
import org.video.mine.adapter.MovieInfoAdapter;
import org.video.mine.databinding.ThirdFragmentBinding;
import org.video.mine.entity.MovieInfoEntity;
import org.video.mine.network.JsoupGet;
import org.video.mine.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

public class ThirdFragment extends Fragment {

    private ThirdViewModel mViewModel;
    private ThirdFragmentBinding binding;
    private HomeDocModel homeDocModel;
    private ViewModelStoreOwner owner;
    private List<MovieInfoEntity> adapterLists = new ArrayList<>();
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    movieInfoAdapter.notifyDataSetChanged();
                    break;
                case 1:
                    if (movieType!=2) {
                        Toast.makeText(getContext(), movieTypes[movieType-17] + " 没有更多了", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(getContext(), "热播电影 没有更多了", Toast.LENGTH_SHORT).show();
                    }
                    binding.srf.finishLoadMore();
                    break;
            }
        }
    };
    private int movieType=2;

    private String[] movieTypes = new String[]{
            "国产剧",//movieType 17
            "香港剧",//18
            "欧美剧",//19
            "日本剧",//20
            "海外剧",//21
            "台湾剧",//22
            "韩国剧"//23
    };
    private String[] movieTypeUrl = new String[]{
            "http://www.sanylvy.com/index.php?s=home-vod-type-id-%s-picm-1-p-%s",
            "http://www.sanylvy.com/index.php?s=home-vod-type-id-%s-picm-1-p-%s",
            "http://www.sanylvy.com/index.php?s=home-vod-type-id-%s-picm-1-p-%s",
            "http://www.sanylvy.com/index.php?s=home-vod-type-id-%s-picm-1-p-%s",
            "http://www.sanylvy.com/index.php?s=home-vod-type-id-%s-picm-1-p-%s",
            "http://www.sanylvy.com/index.php?s=home-vod-type-id-%s-picm-1-p-%s",
            "http://www.sanylvy.com/index.php?s=home-vod-type-id-%s-picm-1-p-%s",
    };

    private int page = 1;
    private int page0 = 1;
    private int page1 = 1;
    private int page2 = 1;
    private int page3 = 1;
    private int page4 = 1;
    private int page5 = 1;
    private int page6 = 1;
    private int page7 = 1;
    private MovieInfoAdapter movieInfoAdapter;
    private String urls;

    public static ThirdFragment newInstance() {
        return new ThirdFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.third_fragment,container,false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        owner = this;
        mViewModel =new ViewModelProvider(this).get(ThirdViewModel.class);
        homeDocModel = new ViewModelProvider(this).get(HomeDocModel.class);
        urls = Urls.dianshi;
        // TODO: Use the ViewModel
        initView();
    }

    private void initView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
        binding.cyclerView.setLayoutManager(gridLayoutManager);
        movieInfoAdapter = new MovieInfoAdapter(adapterLists, getContext(), "second");
        binding.cyclerView.setAdapter(movieInfoAdapter);

        binding.srf.setEnableRefresh(false);
        binding.srf.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageAdd();
                if (isNoData()){
                    pageReduce();
                    mHandler.sendEmptyMessage(1);
                }else {
                    initData();
                }
            }
        });

        initFlexBox();
        initData();
    }

    private boolean isNoData(){
        boolean is = false;
        switch (movieType){
            case 2:
                if (homeDocModel.dianShiPages ==-2){
                    is = true;
                }
                break;
            case 17:
                if (homeDocModel.dianShiPages1 ==-2){
                    is = true;
                }
                break;
            case 18:
                if (homeDocModel.dianShiPages2 ==-2){
                    is = true;
                }
                break;
            case 19:
                if (homeDocModel.dianShiPages3 ==-2){
                    is = true;
                }
                break;
            case 20:
                if (homeDocModel.dianShiPages4 ==-2){
                    is = true;
                }
                break;
            case 21:
                if (homeDocModel.dianShiPages5 ==-2){
                    is = true;
                }
                break;
            case 22:
                if (homeDocModel.dianShiPages6 ==-2){
                    is = true;
                }
                break;
            case 23:
                if (homeDocModel.dianShiPages7 ==-2){
                    is = true;
                }
                break;
        }
        return is;
    }
    private void initFlexBox(){
        for (int i=0; i<movieTypes.length; i++){
            binding.flexLayout.addView(createNewFlexItemTextView(i));
        }
    }

    private TextView createNewFlexItemTextView(final int index) {
        final TextView textView = new TextView(getContext());
        textView.setGravity(Gravity.CENTER);
        textView.setText(movieTypes[index]);
        textView.setTextSize(12);
        textView.setBackground(getResources().getDrawable(R.drawable.round));
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (movieType==index+17){
                    textView.setTextColor(getResources().getColor(R.color.gray));
                    textView.setBackground(getResources().getDrawable(R.drawable.round));
                    movieType = 2;
                }else {
                    if (movieType>2) {
                        TextView childAt = (TextView) binding.flexLayout.getChildAt(movieType-17);
                        childAt.setTextColor(getResources().getColor(R.color.gray));
                        childAt.setBackground(getResources().getDrawable(R.drawable.round));
                    }
                    textView.setTextColor(getResources().getColor(R.color.highLight));
                    textView.setBackground(getResources().getDrawable(R.drawable.round_select));
                    movieType = index+17;
                }
                refreshData();
                Log.d("movieType",movieType+"");
            }
        });
        ViewCompat.setPaddingRelative(textView, 30, 8, 30, 8);
        FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        int margin = ScreenUtils.dp2px(6);
        int marginTop =ScreenUtils.dp2px(5);
        int bottom =ScreenUtils.dp2px(3);
        layoutParams.setMargins(margin, marginTop, margin, bottom);
        textView.setLayoutParams(layoutParams);
        return textView;
    }

    private void refreshData() {
        adapterLists.clear();
        mHandler.sendEmptyMessage(0);
        switch (movieType){
            case 2:
                adapterLists.addAll(homeDocModel.dianShiList);
                page = page0;
                break;
            case 17:
                adapterLists.addAll(homeDocModel.dianShiList1);
                page = page1;
                break;
            case 18:
                adapterLists.addAll(homeDocModel.dianShiList2);
                page = page2;
                break;
            case 19:
                adapterLists.addAll(homeDocModel.dianShiList3);
                page = page3;
                break;
            case 20:
                adapterLists.addAll(homeDocModel.dianShiList4);
                page = page4;
                break;
            case 21:
                adapterLists.addAll(homeDocModel.dianShiList5);
                page = page5;
                break;
            case 22:
                adapterLists.addAll(homeDocModel.dianShiList6);
                page = page6;
                break;
            case 23:
                adapterLists.addAll(homeDocModel.dianShiList7);
                page = page7;
                break;
        }
        if (movieType==2){
            urls = Urls.dianshi;
        }else {
            urls = movieTypeUrl[movieType-17];
        }
        if (adapterLists.size()==0){
            initData();
        }else {
            mHandler.sendEmptyMessage(0);
        }
    }

    private void pageAdd(){
        switch (movieType){
            case 2:
                page0++;
                page = page0;
                break;
            case 17:
                page1++;
                page = page1;
                break;
            case 18:
                page2++;
                page = page2;
                break;
            case 19:
                page3++;
                page = page3;
                break;
            case 20:
                page4++;
                page = page4;
                break;
            case 21:
                page5++;
                page = page5;
                break;
            case 22:
                page6++;
                page = page6;
                break;
            case 23:
                page7++;
                page = page7;
                break;
        }
    }

    private void pageReduce(){
        switch (movieType){
            case 2:
                page0--;
                page = page0;
                break;
            case 17:
                page1--;
                page = page1;
                break;
            case 18:
                page2--;
                page = page2;
                break;
            case 19:
                page3--;
                page = page3;
                break;
            case 20:
                page4--;
                page = page4;
                break;
            case 21:
                page5--;
                page = page5;
                break;
            case 22:
                page6--;
                page = page6;
                break;
            case 23:
                page7--;
                page = page7;
                break;
        }
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                setAdapterLists(JsoupGet.getDianYing(owner, urls,page,movieType,2));
            }
        }).start();
    }

    private void setAdapterLists(List<MovieInfoEntity> lists){
        adapterLists.clear();
        switch (movieType){
            case 2:
                homeDocModel.dianShiList.addAll(lists);
                adapterLists.addAll(homeDocModel.dianShiList);
                break;
            case 17:
                homeDocModel.dianShiList1.addAll(lists);
                adapterLists.addAll(homeDocModel.dianShiList1);
                break;
            case 18:
                homeDocModel.dianShiList2.addAll(lists);
                adapterLists.addAll(homeDocModel.dianShiList2);
                break;
            case 19:
                homeDocModel.dianShiList3.addAll(lists);
                adapterLists.addAll(homeDocModel.dianShiList3);
                break;
            case 20:
                homeDocModel.dianShiList4.addAll(lists);
                adapterLists.addAll(homeDocModel.dianShiList4);
                break;
            case 21:
                homeDocModel.dianShiList5.addAll(lists);
                adapterLists.addAll(homeDocModel.dianShiList5);
                break;
            case 22:
                homeDocModel.dianShiList6.addAll(lists);
                adapterLists.addAll(homeDocModel.dianShiList6);
                break;
            case 23:
                homeDocModel.dianShiList7.addAll(lists);
                adapterLists.addAll(homeDocModel.dianShiList7);
                break;
        }
        mHandler.sendEmptyMessage(0);
        binding.srf.finishLoadMore();
    }

}
